import { url_config } from './config.js'
export const myRequest = (url, method, data, validateLogon = true) => {
  let header = {}
  let token = uni.getStorageSync('token') || 'none'
  if (validateLogon == true) {
    // 等于true时需要加token
    header = {
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Access-Token': token,
    }
  } else {
    header = {
      'Content-Type': 'application/json;charset=UTF-8',
    }
  }

  return new Promise((resolve, reject) => {
    uni.request({
      url: url_config + url,
      method: method.toUpperCase(),
      data: data,
      dataType: 'json',
      header: header,
      success: (res) => {
        if(res.data.code == 401){
          uni.redirectTo({
              url:'/pages/login/login'
          });
          return
        }
        resolve(res.data)
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}
